home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / amiexpress / source / doors / what / what.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-18  |  16.1 KB  |  585 lines

  1. /*
  2. ** $VER: What.c - Transfer Activities v2.0 (01-Jan-96)
  3. **
  4. ** Main
  5. **
  6. ** (c) Copyright 1994/95 Bobo/Mystic.
  7. **     All Rights Reserved.
  8. **
  9. ** Application: Transfer Activities v2.0
  10. ** Language:    SAS/C v6.50
  11. **
  12. ** This sourcecode is free to use. However, you are not allowed to
  13. ** make any changes at all in this code without permission from
  14. ** the auther. You are not allowed to remove my name from the design.
  15. **
  16. ** This version won't probably be updated. If you are interested in
  17. ** updating WHAT, please contact me.
  18. */
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <proto/dos.h>
  24. #include <proto/icon.h>
  25. #include <proto/exec.h>
  26. #include <exec/semaphores.h>
  27. #include <time.h>
  28. #include "amiexpress.h"
  29. #include "structure.h"
  30.  
  31. UBYTE *version = "$VER: Transfer Activities v"VER" ("DATE")";
  32.  
  33. char *status[]={
  34.   "Beginning DL..",  // ST_BEGDL
  35.   "DL'ing file...",  // ST_DLING
  36.   "Download - OK!",  // ST_DLOK
  37.   "Beginning UL..",  // ST_BEGUL
  38.   "Uploading file",  // ST_ULING
  39.   "Uploaded - OK!",  // ST_ULOK
  40.   "Starting HYDRA",  // ST_HYDRA
  41.   "Hydra UL - OK!",  // ST_HYDULOK
  42.   "Hydra DL - OK!",  // ST_HYDDLOK
  43.   "Hydra UL'ing..",  // ST_HYDULING
  44.   "Hydra DL'ing.."   // ST_HYDDLING
  45.     };
  46.  
  47. struct SinglePort *SingleNode;
  48. struct MultiPort *Nodes;
  49.  
  50. struct Library *IconBase;
  51.  
  52. APTR Singles[32];
  53. char bbspath[100];
  54.  
  55. struct List *GetFiles(char *);
  56. struct List *GetDownloadList(const char *);
  57. char *GetLastEntry(const char *);
  58. void GetPlaypenDir(int,char *);
  59. void OutStatusLine(int,const char *,char,const char *,const char *,int);
  60. void MyDateInsert(struct List *,struct File *);
  61. void ClearList(struct List *);
  62. void getsemaphores(void);
  63. char *CenterText(const char *,char *,int);
  64. char *fixbytes(int);
  65. void end(void);
  66.  
  67. //============================================================================
  68. // Main (start)
  69. //============================================================================
  70. void main(int argc,char **argv)
  71. {
  72. struct List *FileList,*FileListNew;
  73. struct File *FileNode;
  74. int totulbytes=0,totulfiles=0;
  75. int totdlbytes=0,totdlfiles=0;
  76. char temp[100];
  77. char *pt;
  78. int act=0;                      // TRANSFER ACTICIVITES
  79. int i;
  80.  
  81.   // started in CLI (missing node argument)
  82.   if(argc<2) exit(0);
  83.  
  84.   DoorStart(argv[1]);
  85.  
  86.   // missing icon.library
  87.   if((IconBase=OpenLibrary("icon.library",36L))==NULL) end();
  88.  
  89.   putuserstring(177,WRITE,"WHAT v"VER"");
  90.   getuserstring(bbspath,128);
  91.  
  92.   sendmessage("\r\n.----------------------------------------------------------------------------.\r\n");
  93.   sendmessage("WHAT: Transfer Activities v"VER" ["TEXT"] Copyright (c)1994-95 Bobo/Mystic! |\r\n");
  94.   sendmessage("|--v----------------------v----------------v--------------v-----------v------|\r\n");
  95.   sendmessage("|NdUsername (Handle)    | Status/Action  | File(s)      | Filesize  | CPS  |\r\n");
  96.   sendmessage("|--+----------------------+----------------+--------------+-----------+------|\r\n");
  97.  
  98.   putuserstring(150,WRITE,"\tTransfer Activities v"VER" by Bobo/Mystic\n");
  99.   getsemaphores();
  100.  
  101.   for(i=0;i<32;i++)
  102.   {
  103.    ObtainSemaphore((struct SignalSemaphore *)Singles[i]);
  104.    SingleNode=(struct SinglePort *)Singles[i];
  105.  
  106.    if(act>0&&(SingleNode->Status==1||SingleNode->Status==2||(SingleNode->Status==3&&stricmp(SingleNode->Misc1,"AEHYDRA")==NULL)))
  107.    {
  108.     sendmessage("|--+----------------------+----------------+--------------+-----------+------|\r\n");
  109.    }
  110.  
  111.    switch(SingleNode->Status)
  112.    {
  113.     case 1: /* USER IS DOWNLOADING A FILE */
  114.             act++;
  115.  
  116.             if(SingleNode->Misc1[0]==NULL)     // DOWNLOADING NOT STARTED YET
  117.             {
  118.              OutStatusLine(i,SingleNode->Handle,ST_BEGDL,"","",0);
  119.              break;
  120.             }
  121.             sprintf(temp,"%sNode%d/UDLog",bbspath,i);
  122.             if((pt=GetLastEntry(temp))==NULL) break;
  123.  
  124.             FileList=GetDownloadList(pt);
  125.             free(pt);
  126.  
  127.             if(FileList==NULL) break;
  128.  
  129.             FileNode=(struct File *)FileList->lh_Head;
  130.             while(FileNode->en_Node.ln_Succ)
  131.             {
  132.              if(stricmp(FileNode->filename,SingleNode->Misc1)==NULL) OutStatusLine(i,SingleNode->Handle,ST_DLING,FileNode->filename,fixbytes(FileNode->filesize),FileNode->cps);
  133.              else
  134.              {
  135.               totdlbytes+=FileNode->filesize; totdlfiles++;
  136.               OutStatusLine(i,SingleNode->Handle,ST_DLOK,FileNode->filename,fixbytes(FileNode->filesize),FileNode->cps);
  137.              }
  138.              FileNode=(struct File *)FileNode->en_Node.ln_Succ;
  139.             }
  140.  
  141.             ClearList(FileList);
  142.             break;
  143.     case 2: /* USER IS UPLOADING A FILE */
  144.             act++;
  145.  
  146.             if(SingleNode->Misc1[0]==NULL)     // UPLOADING NOT STARTED YET
  147.             {
  148.              OutStatusLine(i,SingleNode->Handle,ST_BEGUL,"","",0);
  149.              break;
  150.             }
  151.  
  152.             GetPlaypenDir(i,temp);
  153.             FileList=GetFiles(temp);
  154.             if(FileList==NULL) break;
  155.  
  156.             FileNode=(struct File *)FileList->lh_Head;
  157.             while(FileNode->en_Node.ln_Succ)
  158.             {
  159.              if(stricmp(FileNode->filename,SingleNode->Misc1)==NULL) OutStatusLine(i,SingleNode->Handle,ST_ULING,FileNode->filename,"",FileNode->cps);
  160.              else
  161.              {
  162.               totulbytes+=FileNode->filesize; totulfiles++;
  163.               OutStatusLine(i,SingleNode->Handle,ST_ULOK,FileNode->filename,fixbytes(FileNode->filesize),FileNode->cps);
  164.              }
  165.              FileNode=(struct File *)FileNode->en_Node.ln_Succ;
  166.             }
  167.  
  168.             ClearList(FileList);
  169.             break;
  170.     case 3: /* USER IS USING A DOOR (AEHYDRA?) */
  171.  
  172.             if(stricmp(SingleNode->Misc1,"AEHYDRA")==NULL)
  173.             {
  174.              act++;
  175.  
  176.              sprintf(temp,"%sNode%d/Partupload",bbspath,i);
  177.  
  178.              FileListNew=GetFiles(temp);
  179.              if(FileListNew)
  180.              {
  181.               sprintf(temp,"%sNode%d/Playpen",bbspath,i);
  182.               FileList=GetFiles(temp);
  183.  
  184.               FileNode=(struct File *)FileList->lh_Head;
  185.               while(FileNode->en_Node.ln_Succ)
  186.               {
  187.                totulbytes+=FileNode->filesize; totulfiles++;
  188.                OutStatusLine(i,SingleNode->Handle,ST_HYDULOK,FileNode->filename,fixbytes(FileNode->filesize),FileNode->cps);
  189.  
  190.                FileNode=(struct File *)FileNode->en_Node.ln_Succ;
  191.               }
  192.               FileNode=(struct File *)FileListNew->lh_TailPred;
  193.               OutStatusLine(i,SingleNode->Handle,ST_HYDULING,FileNode->filename,"",FileNode->cps);
  194.  
  195.               ClearList(FileListNew);
  196.               ClearList(FileList);
  197.              }
  198.  
  199.              sprintf(temp,"%sNode%d/UDLog",bbspath,i);
  200.              if((pt=GetLastEntry(temp))==NULL) break;
  201.  
  202.              FileList=GetDownloadList(pt);
  203.              free(pt);
  204.  
  205.              if(FileList==NULL) break;
  206.  
  207.              FileNode=(struct File *)FileList->lh_Head;
  208.              while(FileNode->en_Node.ln_Succ)
  209.              {
  210.               if(FileList->lh_TailPred==(struct Node *)FileNode) OutStatusLine(i,SingleNode->Handle,ST_HYDDLING,FileNode->filename,fixbytes(FileNode->filesize),FileNode->cps);
  211.               else
  212.               {
  213.                totdlbytes+=FileNode->filesize; totdlfiles++;
  214.                OutStatusLine(i,SingleNode->Handle,ST_HYDDLOK,FileNode->filename,fixbytes(FileNode->filesize),FileNode->cps);
  215.               }
  216.               FileNode=(struct File *)FileNode->en_Node.ln_Succ;
  217.              }
  218.              ClearList(FileList);
  219.             }
  220.             break;
  221.    }
  222.    ReleaseSemaphore((struct SignalSemaphore *)Singles[i]);
  223.   }
  224.  
  225.   if(act==0) sendmessage("|          - NO TRANSFER ACTIVITIES -                                        |\r\n");
  226.  
  227.   sendmessage("|--^----------------------^----------------^--------------^-----------^------|\r\n");
  228.   sendmessage("Upload   Activities -> Total files%3d ]   Total bytes%11s ] |\r\n",totulfiles,fixbytes(totulbytes));
  229.   sendmessage("Download Activities -> Total files%3d ]   Total bytes%11s ] |\r\n",totdlfiles,fixbytes(totdlbytes));
  230.   sendmessage("`----------------------------------------------------------------------------'\r\n\r\n");
  231.  
  232.   CloseLibrary(IconBase);
  233.   end();
  234. }
  235.  
  236. //============================================================================
  237. // Output status lines
  238. //============================================================================
  239. void OutStatusLine(int node,const char *un,char action,const char *file,const char *size,int cps)
  240. {
  241. char cpsrate[100];
  242. char sc=32;      // 32 = SPACE
  243.  
  244.   strcpy(cpsrate," -");
  245.  
  246.   switch(action)
  247.   {
  248.    case ST_ULING   :
  249.    case ST_HYDULING: if(cps>0) stci_d(cpsrate,cps);
  250.    case ST_DLING   :
  251.    case ST_HYDDLING: sc='*';
  252.                      break;
  253.    case ST_BEGDL   :
  254.    case ST_BEGUL   :
  255.    case ST_HYDRA   : sc='#';
  256.                      break;
  257.    default: sc=32;
  258.             break;
  259.   }
  260.  
  261.   sendmessage("|%2d%-20.20s |%c%-14s | %-12.12s | %9s | %-4s |\r\n",node,un,sc,status[action],file,size,cpsrate);
  262. }
  263.  
  264. //============================================================================
  265. // Get Playpen Directory
  266. //============================================================================
  267. void GetPlaypenDir(int node,char *dt)
  268. {
  269. struct DiskObject *bbsicon;
  270. char temp[100];
  271. char *str;
  272.  
  273.   sprintf(temp,"%sNode%d",bbspath,node);
  274.   if((bbsicon=GetDiskObject(temp)))
  275.   {
  276.    if((str=FindToolType(bbsicon->do_ToolTypes,"PLAYPEN"))) strcpy(dt,str);
  277.    FreeDiskObject(bbsicon);
  278.   }
  279.   if(str==NULL) sprintf(dt,"%sNode%d/Playpen",bbspath,node);
  280. }
  281.  
  282. //============================================================================
  283. // Get downloads from callerslog/udlog
  284. //============================================================================
  285. struct List *GetDownloadList(const char *pt)
  286. {
  287. struct List *mylist;
  288. struct File *mynode;
  289. char temp[100];
  290. char file[100];
  291. char filepth[100];
  292. char *pos=NULL;
  293. int i=0,x,tmp,len;
  294.  
  295.   while(pt[i])
  296.   {
  297.    for(tmp=i,x=0;pt[i]!='\n'&&pt[i]!=NULL;i++,x++) temp[x]=pt[i];
  298.    temp[x]=NULL; i++;
  299.  
  300.    if((pos=strstr(temp,"Downloading"))!=NULL)
  301.    {
  302.     while(pt[i])
  303.     {
  304.      for(x=0;pt[i]!='\n'&&pt[i]!=NULL;i++,x++) temp[x]=pt[i];
  305.      temp[x]=NULL; i++;
  306.  
  307.      if(strstr(temp,"Downloading")==NULL) { tmp=NULL; break; }
  308.     }
  309.     if(pt[i]==NULL) break;
  310.    }
  311.    tmp=NULL;
  312.   }
  313.  
  314.   if(tmp==NULL) return(NULL);
  315.  
  316.   mylist=calloc(1,sizeof(struct List));
  317.   NewList(mylist);
  318.  
  319.   while(pt[tmp])
  320.   {
  321.    for(x=0;pt[tmp]!='\n'&&pt[tmp]!=NULL;tmp++,x++) temp[x]=pt[tmp];
  322.    temp[x]=NULL; tmp++;
  323.  
  324.    for(i=13,x=0;temp[i]!=' '&&temp[i];i++,x++) filepth[x]=temp[i];
  325.    filepth[x]=NULL;
  326.  
  327.    if(temp[i]==NULL) continue;
  328.  
  329.    stcgfn(file,filepth);
  330.    strupr(file);
  331.  
  332.    len=atoi(&temp[++i]);
  333.    if(len<=0||strlen(file)<=0) continue;
  334.  
  335.    mynode=calloc(1,sizeof(struct File));
  336.    strcpy(mynode->filename,file);
  337.    mynode->filesize=len;
  338.    mynode->date=mynode->cps=0;
  339.  
  340.    AddTail(mylist,(struct Node *)mynode);
  341.   }
  342.  
  343.   return(mylist);
  344. }
  345.  
  346. //============================================================================
  347. // Get the last entry in callerslog (udlog)
  348. //============================================================================
  349. char *GetLastEntry(const char *log)
  350. {
  351. FILE *fp;
  352. char *pt;
  353. char temp[100];
  354. int readlen=0;
  355. int length=0;
  356. int pos=0;
  357. char *p;
  358.  
  359.   if((fp=fopen(log,"rb"))==NULL) return(NULL);
  360.  
  361.   fseek(fp,0,SEEK_END);
  362.   length=ftell(fp);
  363.  
  364.   pos=(length-62);
  365.   if(pos<=100) { fclose(fp); return(NULL); }
  366.  
  367.   fseek(fp,pos,SEEK_SET);
  368.  
  369.   while(1)
  370.   {
  371.    fgets(temp,sizeof(temp),fp);
  372.  
  373.    p=strchr(temp,'\n');
  374.    if(p==NULL) break;
  375.  
  376.    if(temp[0]=='*'&&*(p-1)=='*')
  377.    {
  378.     readlen=length-ftell(fp);
  379.     if(readlen<=0) break;
  380.  
  381.     pt=malloc(readlen+1);
  382.     if(pt==NULL) { pos=0; break; }
  383.  
  384.     fread(pt,readlen,1,fp);
  385.     pt[readlen]=NULL;
  386.  
  387.     break;
  388.    }
  389.  
  390.    pos-=62;
  391.    if(pos<=0) break;
  392.  
  393.    fseek(fp,pos,SEEK_SET);
  394.   }
  395.   fclose(fp);
  396.  
  397.   if(readlen<=0) return(NULL);
  398.   return(pt);
  399. }
  400.  
  401. //============================================================================
  402. // Get a directory sorted after date into a list
  403. //============================================================================
  404. struct List *GetFiles(char *d)
  405. {
  406. struct List *mylist;
  407. struct File *mynode;
  408. struct FileInfoBlock info;
  409. BPTR FLock;
  410. long t;
  411.  
  412.   if((FLock=Lock(d,SHARED_LOCK))==NULL||(Examine(FLock,&info))==NULL)
  413.   {
  414.    if(FLock) UnLock(FLock);
  415.    return(NULL);
  416.   }
  417.  
  418.   mylist=calloc(1,sizeof(struct List));
  419.   NewList(mylist);
  420.  
  421.   if(info.fib_DirEntryType>0)
  422.   {
  423.    while(ExNext(FLock,&info))
  424.    {
  425.     if(info.fib_DirEntryType<0)
  426.     {
  427.      mynode=calloc(1,sizeof(struct File));
  428.      strcpy(mynode->filename,info.fib_FileName);
  429.      strupr(mynode->filename);
  430.  
  431.      mynode->filesize=info.fib_Size;
  432.      mynode->date=(info.fib_Date.ds_Days*86400)+(info.fib_Date.ds_Minute*60)+(info.fib_Date.ds_Tick/50);
  433.  
  434.      MyDateInsert(mylist,mynode);
  435.     }
  436.    }
  437.   }
  438.   UnLock(FLock);
  439.  
  440.   if(IsListEmpty(mylist)) { free(mylist); return(NULL); }
  441.  
  442.   time(&t);
  443.   t-=252482400;
  444.  
  445.   mynode=(struct File *)mylist->lh_Head;
  446.   while(mynode->en_Node.ln_Succ)
  447.   {
  448.    if(mylist->lh_TailPred==(struct Node *)mynode)
  449.    {
  450.     mynode->cps=(t-mynode->date);
  451.     mynode->cps=(mynode->filesize/mynode->cps);
  452.    }
  453.    else mynode->cps=0;
  454.  
  455.    mynode=(struct File *)mynode->en_Node.ln_Succ;
  456.   }
  457.   return(mylist);
  458. }
  459.  
  460. //============================================================================
  461. // Date insert in list
  462. //============================================================================
  463. void MyDateInsert(struct List *mylist,struct File *mynode)
  464. {
  465. struct File *tmp;
  466.  
  467.   if(IsListEmpty(mylist)) AddHead(mylist,(struct Node *)mynode);
  468.   else
  469.   {
  470.    tmp=(struct File *)mylist->lh_Head;
  471.    while(tmp->en_Node.ln_Succ)
  472.    {
  473.     if(mynode->date<tmp->date)
  474.     {
  475.      Insert(mylist,(struct Node *)mynode,tmp->en_Node.ln_Pred);
  476.      break;
  477.     }
  478.     tmp=(struct File *)tmp->en_Node.ln_Succ;
  479.    }
  480.    if(tmp->en_Node.ln_Succ==NULL) AddTail(mylist,(struct Node *)mynode);
  481.   }
  482. }
  483.  
  484. //============================================================================
  485. // Clear a list
  486. //============================================================================
  487. void ClearList(struct List *mylist)
  488. {
  489. struct Node *mynode,*tmp;
  490.  
  491.   if(mylist)
  492.   {
  493.    if(IsListEmpty(mylist)==NULL)
  494.    {
  495.     mynode=(struct Node *)mylist->lh_Head;
  496.  
  497.     while(mynode->ln_Succ)
  498.     {
  499.      tmp=(struct Node *)mynode->ln_Succ;
  500.      free(mynode);
  501.      mynode=tmp;
  502.     }
  503.    }
  504.    free(mylist);
  505.    mylist=NULL;
  506.   }
  507. }
  508.  
  509. //============================================================================
  510. // Get semaphore pointers
  511. //============================================================================
  512. void getsemaphores(void)
  513. {
  514. int i;
  515.  
  516.   putuserstring(531,READ,"");    // GET SEMEPHORE POINTER TO MSG STRUCTURE
  517.   Nodes=(struct MultiPort *)msg.Semi;
  518.   ObtainSemaphore((struct SignalSemaphore *)Nodes);
  519.  
  520.   for(i=0;i<32;i++) Singles[i]=Nodes->MyNode[i].s;
  521.  
  522.   ReleaseSemaphore((struct SignalSemaphore *)Nodes);
  523. }
  524.  
  525. //============================================================================
  526. // Center a string
  527. //============================================================================
  528. char *CenterText(const char *s,char *d,int len)
  529. {
  530. int i,x,pos;
  531.  
  532.   if(s==NULL||d==NULL||len<=0) return(NULL);
  533.  
  534.   pos=(len/2)-(strlen(s)/2);
  535.   if(pos<0) return(NULL);
  536.  
  537.   for(i=0;i<pos;i++) d[i]=32;
  538.  
  539.   for(x=0;s[x];x++,i++) d[i]=s[x];
  540.  
  541.   for(;i<len;i++) d[i]=32;
  542.   d[i]=NULL;
  543.  
  544.   return(d);
  545. }
  546.  
  547. //============================================================================
  548. // Fix upload bytes
  549. // Routine by LOOBY/INSANE
  550. //============================================================================
  551. char *fixbytes(int byte)
  552. {
  553. static char temp[100];
  554. char byt[15];
  555. int len,x,mark;
  556.  
  557.   stcul_d(byt,byte);
  558.   len=strlen(byt);
  559.  
  560.   if(len<=3) mark=len-1;
  561.   else
  562.   if(len<=6) mark=len;
  563.   else
  564.   if(len<=9) mark=len+1;
  565.   else mark=len+2;
  566.  
  567.   temp[mark+1]=NULL;
  568.  
  569.   for(x=--len;x>=0;x--)
  570.   {
  571.    temp[mark--]=byt[x];
  572.    if((x==len-2||x==len-5||x==len-8)&&x!=0) temp[mark--]='.';
  573.   }
  574.   return((char *)&temp[0]);
  575. }
  576.  
  577. //============================================================================
  578. // End (quit and return to express)
  579. //============================================================================
  580. void end(void)
  581. {
  582.   CloseDoor();
  583.   exit(0);
  584. }
  585.